﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;

namespace YJKArch.NumberRoom
{
	public class Common
	{
		public static bool HasTagOfRoom(Autodesk.Revit.DB.Document doc, ElementId viewId, Room room)
		{
			return Common.GetTagsOfRoom(doc, viewId, room).Count > 0;
		}

		public static List<ElementId> GetTagsOfRoom(Autodesk.Revit.DB.Document doc, ElementId viewId, Room room)
		{
			List<ElementId> list = new List<ElementId>();
			IEnumerable<RoomTag> enumerable = from elem in new FilteredElementCollector(doc, viewId).WherePasses(new RoomTagFilter()).ToElements()
			let roomTag = elem as RoomTag
			where roomTag != null && roomTag.Room != null
			select roomTag;
			if (enumerable != null)
			{
				foreach (RoomTag roomTag in enumerable)
				{
					Room room2 = roomTag.Room;
					if (room.Id.Compare(room2.Id) == 0)
					{
						list.Add(roomTag.Id);
					}
				}
			}
			return list;
		}

		public static bool HasSameNameRoomTag(Autodesk.Revit.DB.Document doc, ElementId viewId, string tagName)
		{
            IEnumerable<Room> enumerable = from elem in new FilteredElementCollector(doc, viewId).WherePasses(new Autodesk.Revit.DB.Architecture.RoomFilter()).ToElements()
			let theRoom = elem as Room
			where theRoom != null
			select theRoom;
			if (enumerable == null)
			{
				return false;
			}
			IEnumerator<Room> enumerator = enumerable.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (Common.GetRoomName(enumerator.Current).CompareTo(tagName) == 0)
					{
						return true;
					}
				}
			}
			return false;
		}

		public static void ChangeToTempNameWithRoom(Autodesk.Revit.DB.Document doc, IList<Reference> selRefs)
		{
			int num = 1;
			foreach (Reference reference in selRefs)
			{
				Room room = doc.GetElement(reference) as Room;
				if (room != null)
				{
					string strName = "HY_TempName_" + num.ToString();
					Common.SetRoomName(room, strName);
					num++;
				}
			}
		}

		public static List<Room> GetRooms(Autodesk.Revit.DB.Document doc, ElementId viewId)
		{
			List<Room> list = new List<Room>();
			IEnumerable<Room> enumerable = from elem in new FilteredElementCollector(doc, viewId).WherePasses(new Autodesk.Revit.DB.Architecture.RoomFilter ()).ToElements()
			let theRoom = elem as Room
			where theRoom != null
			select theRoom;
			if (enumerable != null)
			{
				foreach (Room item in enumerable)
				{
					list.Add(item);
				}
			}
			return list;
		}

		public static void CreateRoomTag(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View view, Room room, XYZ pt, string tagName)
		{
			XYZ point = (room.Location as LocationPoint).Point;
			UV point2 = new UV(point.X, point.Y);
			List<ElementId> tagsOfRoom = Common.GetTagsOfRoom(doc, view.Id, room);
			if (tagsOfRoom.Count > 0)
			{
				foreach (ElementId elementId in tagsOfRoom)
				{
					doc.Delete(elementId);
				}
			}
			Common.SetRoomName(room, tagName);
			RevitVersionFuncs.NewRoomTag(doc, room, point2, view);
		}

		public static void SetRoomName(Room room, string strName)
		{
			room.get_Parameter(BuiltInParameter.ROOM_NAME).Set(strName);
		}

		public static string GetRoomName(Room room)
		{
			return room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString();
		}

		public static bool ConvertToInt(string str, ref int value)
		{
			try
			{
				value = int.Parse(str);
			}
			catch (FormatException)
			{
				return false;
			}
			return true;
		}

		public static void UpdateModel(UIDocument uiDoc, bool autoJoin)
		{
			uiDoc.Document.Regenerate();
			if (autoJoin)
			{
				uiDoc.Document.AutoJoinElements();
			}
			uiDoc.RefreshActiveView();
		}
	}
}
